home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / hzip.com / PNODE.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  2.0 KB  |  64 lines

  1. ////////////////////////////////////////////////////////////
  2. // Package node class header file
  3. // Copyright (c) 1991 by Azarona Software
  4. // All rights reserved.
  5. ////////////////////////////////////////////////////////////
  6.  
  7. #ifndef H_PNODE
  8. #define H_PNODE
  9.  
  10. // We use our own custom allocator for pnodes, cause
  11. // it's much faster and takes less memory than using
  12. // Turbo C++'s generic new and delete routines
  13.  
  14. struct anode {       // allocator node for pnodes
  15.   int node_cnt;      // sizeof(node_cnt) + sizeof(pnode_data)
  16.   int pnode_data[5]; // must >= sizeof(pnode)
  17.   unsigned n;        // Pointer to next pnode on free list
  18. };
  19.  
  20. struct pnode_allocator {
  21.   anode *data;
  22.   int size;
  23.   pnode_allocator(int sz);
  24.   ~pnode_allocator(void) { delete[size] data; }
  25.   void *alloc(void);
  26.   void free(void *p);
  27.   int room(void) { return size - data->node_cnt; }
  28. };
  29.  
  30. // Package node class. Note how new() and delete()
  31. // are overloaded
  32.  
  33. struct pnode {       // package node
  34.   long wt;           // weight of the node
  35.   pnode *n;          // to other members in the package
  36.   unsigned char sym; // which symbol this node is for
  37.   signed char lvl;   // what level that symbol is at (-1 means internal)
  38.   static pnode_allocator *pal;
  39.   pnode(long w, unsigned char s, signed char l, pnode *nx) {
  40.      wt = w; sym = s; lvl = l; n = nx;
  41.   }
  42.   void *operator new(size_t)    { return (void *)pal->alloc(); }
  43.   void operator delete(void *p) { pal->free(p); }
  44.   static pnode *make_package(pnode *a, pnode *b);
  45.   static void rmv_package(pnode *p);
  46. };
  47.  
  48. // Set of package nodes, represented as an array of pointers
  49.  
  50. struct nodeset {
  51.   pnode **data;
  52.   int sz, cursor;
  53.   nodeset(int ns) { data = new pnode*[sz = ns*2]; cursor = 0; }
  54.   ~nodeset(void)  { delete[sz] data;                          }
  55.   pnode *remove_front(void);
  56.   void package(void);
  57.   void setup_and_merge(nodeset &nset, long wts[], unsigned char smap[], 
  58.                        int num_syms, int level);
  59.   int isempty(void) { return cursor == 0; }
  60. };
  61.  
  62. #endif
  63.  
  64.